// pages/vip/vip.js
const api = require("../../request/api")
const request = require("../../request/request")
const util = require("../../utils/util")
const App = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    avatar_url: 'https://thirdwx.qlogo.cn/mmopen/vi_32/POgEwh4mIHO4nibH0KlMECNjjGxQUq24ZEaGT4poC6icRiccVGKSyXwibcPq4BWmiaIGuG1icwxaQX6grC9VemZoJ8rg/132',
    user_name: '请点击登录',
    if_register: false,
    uid: '',
    user_info: null,
    more_list: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.get_more_list() 
    this.init_data()
  },
  init_data() {
    this.setData({
      uid: util.getAppValue(App.globalData.uid),
      if_register: App.globalData.if_register,
      user_info: App.globalData.userInfo,
    })
    if (this.data.user_info.avatarUrl) {
      this.setData({
        avatar_url:this.data.user_info.avatarUrl,
        user_name: this.data.user_info.nickName
      })
    }
  },
  go_detail_from_component(e) {
    let id = e.detail.currentTarget.dataset.id
    if (!id) {
      id = 10
    }
    wx.navigateTo({
      url: '/pages/good/detail?id=' + id,
    })
  },
  get_more_list() {
    let params = {
      uid: this.data.uid
    }
    util.showLoading('加载中')
    request.post(api.goods_more, params).then(res=>{
      util.hideLoading()
      this.setData({
        more_list: res
      })
    })
  },
  cart_add(){
    util.showLoading("正在添加")
    let params = {
      // goods_num: this.data.cart_num,
      goods_id: this.data.goods_id,
      uid: this.data.uid
    }
    request.post(api.cart_add, params).then(res=>{
      this.setData({
        if_show_box:false
      })
      util.showToast("已加入购物车")
    })
  },
  go_login() {
    wx.navigateTo({
      url: '/pages/login/login',
    })
  },
  go_buy_vip() {
    if (!this.data.if_register) {
      util.showToast("请先登录")
      return
    }
    let url = '/pages/vip/buy_vip'
    wx.navigateTo({
      url,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})